"""
需求：老板雇佣了一个工人，让他生产杯子，老板一直等着工人，直到生产了10个杯子。
"""

from threading import Event, Thread
import logging
import time

# 全局设置
# 定格日志格式。
FORMAT = "%(asctime)s %(thread)d %(message)s"
# 定格日志级别，INFO是信息级别；调用日志格式。
logging.basicConfig(level=logging.INFO, format=FORMAT)


def boss(event: Event):
    logging.info("i am boss, waitting for you")
    # 等待
    event.wait()
    logging.info("good job")


def worker(event: Event, count=10):
    logging.info("i am worker,working...")
    cups = []
    while True:
        logging.info("make 1")
        time.sleep(1)
        cups.append(1)
        if len(cups) >= count:
            # 通过
            event.set()
            break
    logging.info('i finished my job, the numbers of cpus is {}'.format(cups))


event = Event()

w = Thread(target=worker, args=(event,)).start()
b = Thread(target=boss, args=(event,)).start()


"""
总结：使用同一个Event对象的标记flag。谁wait就是等到flag为True，或者等到超时返回False。不限制等待的个数。
"""